package com.gz.cookie;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/userUiServlet")
public class UserUiServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //添加cookie给账号
        String username="";
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) {
                    username = cookie.getValue();
                }
            }
        }
        System.out.println("UserUiServlet的doPost被调用");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
            writer.println("<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <title>Title</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "<form action=\"loginServlet\" target=\"_blank\">\n" +
                    "  <h1>用户登录</h1>\n" +
                    "  用户名<input type=\"text\" name=\"username\" value=\""+username+"\"><br>\n" +
                    "  密码<input type=\"password\" name=\"pwd\"><br>\n" +
                    "    <input type=\"submit\" value=\"提交\">\n" +
                    "</form>\n" +
                    "</body>\n" +
                    "</html>");
        writer.close();
    }
}
